package com.feihao.zhou.web.controller.story;

import com.feihao.zhou.bean.story.Catalog;
import com.feihao.zhou.bean.story.Course;
import com.feihao.zhou.service.story.CatalogService;
import com.feihao.zhou.service.story.CourseService;
import com.feihao.zhou.service.story.Impl.CatalogServiceImpl;
import com.feihao.zhou.service.story.Impl.CourseServiceImpl;
import com.feihao.zhou.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/store/catalog")
public class CatalogServlet extends HttpServlet {
    static CatalogService catalogService = new CatalogServiceImpl();
    static CourseService courseService = new CourseServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text.html;charset=utf-8");

        //获取请求option：
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            //查询方法：
            findAll(req,resp);
        }else if ("toAdd".equals(operation)){
            add(req,resp);
        }else if ("save".equals(operation)){
            sava(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            update(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        catalogService.delete(id);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //重定向：
        try {
            resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class, "yyyy-MM-dd");

        System.out.println(catalog);

        catalogService.update(catalog);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //重定向：
        try {
            resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Catalog catalog = catalogService.findById(id);

        List<Course> all = courseService.findAll();
        req.setAttribute("courseList",all);

        req.setAttribute("catalog",catalog);
        req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/store/catalog/update.jsp").forward(req,resp);
    }

    private void sava(HttpServletRequest req, HttpServletResponse resp) {
        Catalog catalog = BeanUtil.fillBean(req, Catalog.class, "yyyy-MM-dd");
        catalogService.save(catalog);
        //重定向：
        try {
            resp.sendRedirect(req.getContextPath()+"/store/catalog?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        List<Course> all = courseService.findAll();
        req.setAttribute("courseList",all);
        //重定向到添加页面：
        req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/store/catalog/add.jsp").forward(req,resp);

    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先定义page,pageSize:
        int page = 1;
        int pageSize = 5;

        if(StringUtils.isNotBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }

        //调用业务层方法查询所有：
        PageInfo all = catalogService.findAll(page, pageSize);

        System.out.println(all);
        req.setAttribute("page",all);
        req.getRequestDispatcher("/WEB-INF/store/catalog/list.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
